package com.fingard.dsp.bank.directbank.lcbip01;

import java.io.File;

import com.fingard.FGBiz;
import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.ISinglePay;
import com.fingard.dsp.bank.reqrespobj.single.*;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.io.FileHelper;
import com.fingard.net.ConnState;
import com.fingard.util.FtpUtil;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;


/**
 * Created by F on 2018/9/13.
 */
public class LCBIP01SinglePay extends LCBIP01Base implements ISinglePay {

    /**
     * 支付
     * @param p_req1908
     * @return
     * @throws Exception
     */
    @Override
    public RetAtsSingle d_1908_SglPay(ReqSingle p_req1908) throws Exception {
        RetAtsSingle retAtsSingle = new RetAtsSingle(p_req1908.reqHeader);
        RetAtsSglDetail detail = new RetAtsSglDetail();
        try{
            //拼接请求报文头
            XmlTextWriter xmlTextWriter = new XmlTextWriter();
            LCBIP01Header header=new LCBIP01Header();
            header.TuxName="Pay";
            header.ReqSeqNo = getQryTransID();
            singleNumMapping(p_req1908.singleDetail.askNumber,header.ReqSeqNo);//流水号关联映射
            writeCommonHeader(xmlTextWriter,header);
            xmlTextWriter.writeElementString("ReqReserved1","");
            xmlTextWriter.writeElementString("ReqReserved2","");
            xmlTextWriter.writeElementString("ReqReserved3","");
            xmlTextWriter.writeElementString("ReqReserved4","");
            xmlTextWriter.writeEndElement();
            //拼接请求报文体
            XElement body = new XElement("body","");
            body.addChild(new XElement("CustCode",bankFront.org));//客户编号
            body.addChild(new XElement("Amt",p_req1908.singleDetail.payAmount));//付款金额
            body.addChild(new XElement("PayAccNo",p_req1908.singleDetail.payAct));//付款帐号
            body.addChild(new XElement("PayAccName",p_req1908.singleDetail.payName));//付款帐号名称
            String areaName=p_req1908.singleDetail.payAreaName;
            if(!"".equals(areaName)){
                body.addChild(new XElement("PayPriv",areaName.substring(0,areaName.indexOf("_"))));//付款帐号开户省份名称
                body.addChild(new XElement("PayCity",areaName.substring(areaName.indexOf("_")+1)));//付款帐号开户城市名称
            }else{
                body.addChild(new XElement("PayPriv",""));//付款帐号开户省份名称
                body.addChild(new XElement("PayCity",""));//付款帐号开户城市名称
            }
            body.addChild(new XElement("PayBankName",p_req1908.singleDetail.payBankName));//付款帐号开户银行名称
            body.addChild(new XElement("RcvAccNo",p_req1908.singleDetail.recAct));//收款帐号
            body.addChild(new XElement("RcvAccName",p_req1908.singleDetail.recName));//收款帐号名称
            body.addChild(new XElement("RcvBankName",p_req1908.singleDetail.cnapsName));//收款银行名称
            String recAreaName=p_req1908.singleDetail.recAreaName;
            if(!"".equals(recAreaName)){
                body.addChild(new XElement("RcvPriv",recAreaName.substring(0,recAreaName.indexOf("_"))));//收款帐号开户省份名称
                body.addChild(new XElement("RcvCity",recAreaName.substring(recAreaName.indexOf("_")+1)));//收款帐号开户城市名称
            }else{
                body.addChild(new XElement("RcvPriv",""));//收款帐号开户省份名称
                body.addChild(new XElement("RcvCity",""));//收款帐号开户城市名称
            }
            body.addChild(new XElement("CurType",convertCurToBank(p_req1908.singleDetail.payCur)));//付款金额对应的核算币种
            body.addChild(new XElement("ReMark",p_req1908.singleDetail.purpose));//付款备注
            body.addChild(new XElement("PostInfo",p_req1908.singleDetail.description));//交易附言
            body.addChild(new XElement("BillNo",""));//付款单号
            body.addChild(new XElement("RcvBIC",p_req1908.singleDetail.cnapsCode));//收款银行行号
            body.addChild(new XElement("RcvBICSource","1"));//收款银行联行号来源
            body.addChild(new XElement("ReqReserved1",""));//备用字段
            body.addChild(new XElement("ReqReserved2",""));//备用字段
            body.addChild(new XElement("ReqReserved3",""));//备用字段
            body.addChild(new XElement("ReqReserved4",""));//备用字段
            nodeAnalysis(p_req1908.singleDetail.srcXElement,body,getCharset());
            xmlTextWriter.writeXElement(body);
            xmlTextWriter.writeEndDocument();

            //发送请求报文
            String[] bankResult = sendToBank(xmlTextWriter.toXmlString());

            //处理响应报文
            if(bankResult[0].length()==0){
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader xmlSbReader = loadBankRespXmlReader(bankResult[1],tmpSbMsg);
                if(xmlSbReader!=null){
                    detail.respCode = "0";
                    detail.respInfo = xmlSbReader.getFirstText("/lcbank/head/RespInfo");
                    detail.reqSeqID = xmlSbReader.getFirstText("/lcbank/head/ReqSeqNo");
                    detail.payInfoCode = xmlSbReader.getFirstText("/lcbank/head/RespCode");
                    detail.transState = detail.payInfoCode;
                    detail.transAmt = "";
                    detail.outTransID = xmlSbReader.getFirstText("/lcbank/head/ReqSeqNo");
                }else{
                    detail.respCode ="-1";
                    detail.respInfo = tmpSbMsg.toString();
                }
            }else if (bankResult[0].equals(ConnState.FAIL)) {
                detail.respCode = "0";
                detail.respInfo = bankResult[1];
                detail.reqSeqID = p_req1908.singleDetail.askNumber;
                detail.transState = PaymentState.FAIL_Payment;
                detail.payInfoCode = bankResult[0];
            }else{
                detail.respCode ="-1";
                detail.respInfo = bankResult[1];
                detail.reqSeqID = p_req1908.singleDetail.askNumber;
            }
        }catch (Exception e){
            detail.respCode = "-1";
            detail.respInfo = e.getMessage();
            e.printStackTrace();
        }
        retAtsSingle.writeRetDetail(detail);
        return retAtsSingle;
    }

    @Override
    public RetAtsSingleBat d_1908BAT_SglBatPay(ReqSingleBat p_req1908BAT) throws Exception {
        return null;
    }

    /**
     * 查询支付结果
     * @param p_req1944
     * @return
     * @throws Exception
     */
    @Override
    public RetAtsSingle d_1944_QrySglPay(ReqQrySingle p_req1944) throws Exception {
        RetAtsSingle retAtsSingle = new RetAtsSingle(p_req1944.reqHeader);
        RetAtsSglDetail detail = new RetAtsSglDetail();
        try{
            //拼接报文头
            XmlTextWriter xmlTextWriter = new XmlTextWriter();
            LCBIP01Header header=new LCBIP01Header();
            header.TuxName="PayStatus";
            header.ReqSeqNo=getQryTransID();
            writeCommonHeader(xmlTextWriter,header);
            xmlTextWriter.writeElementString("PayAccNo", p_req1944.payAct);
            String payDate=Format.Date8Format.format(Format.DateTimeFormat.parse(p_req1944.bgnDate));
            if(payDate.isEmpty()||p_req1944.askNumber.isEmpty()) {
                detail.respCode = "-1";
                detail.respInfo = "查询时交易日期和客户流水号不能为空";
                retAtsSingle.writeRetDetail(detail);
                return retAtsSingle;
            }
            xmlTextWriter.writeElementString("PayDate", payDate);
            xmlTextWriter.writeElementString("ReqReserved1","");
            xmlTextWriter.writeElementString("ReqReserved2","");
            xmlTextWriter.writeElementString("ReqReserved3","");
            xmlTextWriter.writeElementString("ReqReserved4","");
            xmlTextWriter.writeEndElement();
            //拼接报文体
            XElement body = new XElement("body","");
            body.addChild(new XElement("CustCode",bankFront.org));//客户编号
            //取出流水号关联映射
            String dataFilePath=FGBiz.dspCenter.dspSet.dataDirPath + File.separator +"LCBIPSingle" +File.separator+ p_req1944.askNumber+".txt";
            String fileContent = "";
            if (FileHelper.existFile(dataFilePath)) {
                fileContent = FtpUtil.getTextFromFile(dataFilePath, getCharset());
            }else{
                detail.respCode = "-1";
                detail.respInfo = "不存在该流水号关联映射:"+p_req1944.askNumber;
                retAtsSingle.writeRetDetail(detail);
                return retAtsSingle;
            }
            if(!fileContent.contains(p_req1944.askNumber)){
                detail.respCode = "-1";
                detail.respInfo = "该流水号关联映射错误:"+p_req1944.askNumber;
                retAtsSingle.writeRetDetail(detail);
                return retAtsSingle;
            }
            int askIndex=fileContent.indexOf(p_req1944.askNumber)+p_req1944.askNumber.length();
            body.addChild(new XElement("OrgSeqNo",fileContent.substring(askIndex+1,askIndex+9)));//客户流水号
            body.addChild(new XElement("OreqType","1"));//
            body.addChild(new XElement("OrgBillNo",""));//单据编号
            body.addChild(new XElement("ReqReserved1",""));
            body.addChild(new XElement("ReqReserved2",""));
            body.addChild(new XElement("ReqReserved3",""));
            body.addChild(new XElement("ReqReserved4",""));
            xmlTextWriter.writeXElement(body);
            xmlTextWriter.writeEndDocument();
            String[] bankResult = sendToBank(xmlTextWriter.toXmlString());
            if (bankResult[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader xmlSbReader = loadBankRespXmlReader(bankResult[1], tmpSbMsg);
                if (xmlSbReader != null) {
                    //响应结果
                    String respCode=xmlSbReader.getFirstText("/lcbank/head/RespCode");
                    String respInfo=xmlSbReader.getFirstText("/lcbank/head/RespInfo");
                    if ("0".equals(respCode)) {
                        //交易结果
                        String result = xmlSbReader.getFirstText("/lcbank/body/Result");
                        String descript= xmlSbReader.getFirstText("/lcbank/body/Descript");
                        detail.respCode = "0";
                        detail.respInfo = respInfo;
                        detail.payInfoCode = result;
                        detail.reqSeqID = xmlSbReader.getFirstText("/lcbank/head/ReqSeqNo");
                        detail.transState = detail.payInfoCode;
                        detail.transAmt = "";
                    } else {
                        detail.respCode = "-1";
                        detail.respInfo = respInfo;
                    }
                }else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpSbMsg.toString();
                }
            }else {
                detail.respCode = "-1";
                detail.respInfo = bankResult[1];
            }
        }catch (Exception e){
            detail.respCode = "-1";
            detail.respInfo = e.getMessage();
            e.printStackTrace();
        }
        retAtsSingle.writeRetDetail(detail);
        return retAtsSingle;
    }

    @Override
    public RetAtsSingleBat d_1944BAT_QrySglBatPay(ReqQrySingleBat p_req1944BAT) throws Exception {
        return null;
    }
}
